﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrightIdeasSoftware;
using System.Collections;
using MoneyWentWhere.DataModel;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.ObjectModel;

namespace MoneyWentWhere.WinFormsUI.Reports
{
    class AccountBalanceReconcileReport : ReportType
    {
        protected override Type RowType
        {
            get { return typeof(AccountRow); }
        }

        // Makes all calculations
        protected override ICollection GetViewObjects(MoneyDataSet dataSet)
        {
            var root = new AccountRow("All Accounts");

            var curGroups = dataSet.Accounts.GroupBy(x => x.Currency);
            foreach (var cg in curGroups)
            {
                var cur = new AccountRow(cg.Key.Id + " Accounts");
                cur.AddAllNodesAndCalculate(cg);
                root.Nodes.Add(cur);
            }
            root.CalculateCells();

            return new List<AccountRow>() { root };
        }

        public override void FillView(MoneyDataSet data)
        {
            base.FillView(data);
            
            TreeListView tree = View as TreeListView;
            if (tree != null) { tree.ExpandAll(); }
        }
    }

    class AccountRow : ReportRow<Account>
    {
        [OLVColumn(DisplayIndex=0, FillsFreeSpace=true)]
        public String Name;

        [OLVColumn(DisplayIndex=1, Width=60)]
        public Currency Currency;
        
        [OLVColumn(DisplayIndex=2, Width=100, AspectToStringFormat="{0:#,0.00}", TextAlign=HorizontalAlignment.Right)] 
        public decimal? Balance;

        [OLVColumn(DisplayIndex=3, Width=100, AspectToStringFormat="{0:#,0.00}", TextAlign=HorizontalAlignment.Right)] 
        public decimal? NewBalance { get { return null; } } // TODO: add setter
            
        public decimal? XR;

        [OLVColumn(DisplayIndex=4, Width=100, AspectToStringFormat="${0:#,0}", TextAlign=HorizontalAlignment.Right)] 
        public decimal? DcBalance;

        public AccountRow() { }

        public AccountRow(String name) { Name = name; }

        protected override void CalculateCells(Account item)
        {
            Name = item.Name;
            Currency = item.Currency;
            Balance = item.Balance;

            XR = item.DataSet.GetExchangeRate(
                    Item.Currency,
                    Item.DataSet.DefaultCurrency,
                    DateTime.Now);

            DcBalance = item.Balance * XR;
        }

        protected override void CalculateCells(List<ReportRow<Account>> nodes)
        {
            IEnumerable<AccountRow> aNodes = Nodes.Cast<AccountRow>();

            DcBalance = aNodes.Sum(x => x.DcBalance);
            IEnumerable<Currency> cs = aNodes.Select(x => x.Currency).Distinct();
            if (cs.Count() == 1)
            {
                Currency = cs.First();
                Balance = aNodes.Sum(x => x.Balance);
            }
        }

    }

}
